//
//  WYCTranslationController.m
//  初试
//
//  Created by 中企互联 on 2017/10/16.
//  Copyright © 2017年 李胜军. All rights reserved.
//

#import "WYCPlanController.h"
#import "WYCDTViewCell.h"
#import "WYCHeaderFooterView.h"
#import "WYCTransLationModel.h"
#import "WYCTransDetailModel.h"
#import "WYCPlanModel.h"
#import "WYCPlanDetailModel.h"
#import "WYCPlanHearderFootView.h"
#import "WYCPlandetaiLCell.h"
#import "UIViewController+HUD.h"
@interface WYCPlanController ()<WYCHeaderFooterViewDelegate,WYCPlanHearderFootViewDelegate>
{
    
    NSString * bankName;
    NSString * bankid;
    NSString * planid;
    
}
/** data数组*/
@property(nonatomic,strong)NSMutableArray * dataArray;

/** data数组*/
@property(nonatomic,strong)NSMutableArray * planArray;

@property(nonatomic,assign)BOOL Display;
/** details数组*/
@property(nonatomic,strong)NSMutableArray *detailsArray;

/** 显示View*/
@property(nonatomic,strong)UIView * showView;

/** 删除按钮*/
@property(nonatomic,strong) UIButton * buttonsender;

/** 遮罩*/
@property(nonatomic,strong)UIView * ejectView;

@end

@implementation WYCPlanController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //接口
    
    //初始化列表
    [self setupTableView];
    
  self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(setupRequster)];
    
    
    self.dataArray = [[NSMutableArray alloc] init];
    
    self.detailsArray = [[NSMutableArray alloc] init];
    
    self.planArray = [[NSMutableArray alloc] init];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
       [self setupRequster];

}

#pragma make ----------->>接口
- (void)setupRequster{
    
    [self.dataArray removeAllObjects];
    [self.detailsArray removeAllObjects];
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"token"] = [WYCAccountTool unarchivetoken];
    params[@"cardId"] = self.cardId;
    [HttpTool POST:creditcardrepaymentplanlist params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        WYCLog(@"==========%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSDictionary * dict = responseObject[@"result"];
            NSArray * dataArray = dict[@"data"];
            for (NSDictionary * dic in dataArray) {
                WYCPlanModel * taranLationModel = [WYCPlanModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:taranLationModel];
                
                [self.planArray addObject:taranLationModel.planId];
                NSArray * detailsArray = dic[@"details"];
                
                NSMutableArray * arr = [[NSMutableArray alloc] init];
                for (NSDictionary * detailsDic in detailsArray) {
                    WYCPlanDetailModel * DetailModel = [WYCPlanDetailModel mj_objectWithKeyValues:detailsDic];
                    [arr addObject:DetailModel];
                }
                taranLationModel.dataArray = arr;
            }
        }else{
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
    }];
    
}

- (void)setupTableView
{
    // 设置内边距
    CGFloat bottom = self.tabBarController.tabBar.height;
    CGFloat top = 20;
    self.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom+70, 0);
    self.tableView.sectionHeaderHeight = 220 * px;
    self.tableView.rowHeight = 150 * px;
    self.tableView.backgroundColor = WYCGlobalBg;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 设置滚动条的内边距
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    WYCPlanHearderFootView * head = [WYCPlanHearderFootView headFootViewTableView:tableView section:section];
    head.delegate = self;
    head.group = self.dataArray[section];
    
    UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpGR:)];
    
    //设定最小的长按时间 按不够这个时间不响应手势
    
    longPressGR.minimumPressDuration = 1;
    
    [head addGestureRecognizer:longPressGR];
    //    [self reloadTableView];
    return head;
}

-(void)reloadTableView
{
    [self.tableView reloadData];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    WYCPlanModel * head = self.dataArray[section];
    return  head.isOpen ? head.dataArray.count : 0;
    
    
    
}
-(void)lpGR:(UILongPressGestureRecognizer *)lpGR

{
    WYCLogFunc;
    
    
    if (lpGR.state == UIGestureRecognizerStateBegan) {//手势开始
        CGPoint point = [lpGR locationInView:self.tableView];
        
        NSIndexPath *indexPath=[self.tableView indexPathForRowAtPoint:point];
        NSLog(@"section----%li,----row---%li",(long)indexPath.section,(long)indexPath.row);
        planid = self.planArray[indexPath.section];
        WYCLog(@"========%@",planid);
        
        self.ejectView = [[UIView alloc] init];
        self.ejectView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        self.ejectView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [[UIApplication sharedApplication].keyWindow  addSubview:self.ejectView];
        
        UIView * showView = [[UIView alloc] init];
        showView.frame = CGRectMake(80 * px, ScreenHeight / 2 - 250 * px, ScreenWidth - 160 * px, 500 * px);
        showView.layer.cornerRadius = 3;
        showView.layer.masksToBounds = YES;
        showView.backgroundColor = [UIColor whiteColor];
        [self.ejectView addSubview:showView];
        self.showView = showView;
        
        UIView * topView = [[UIView alloc] init];
        topView.frame = CGRectMake(0, 0, showView.width, 150 * px);
        topView.backgroundColor = [UIColor whiteColor];
        [showView addSubview:topView];
        
        UILabel * showlabel = [[UILabel alloc] init];
        showlabel.frame = CGRectMake(0, 0, topView.width, topView.height);
        showlabel.text = @"终止计划";
        showlabel.textColor = wycMain;
        showlabel.font = [UIFont boldSystemFontOfSize:BigFont];
        showlabel.textAlignment = NSTextAlignmentCenter;
        [topView addSubview:showlabel];
        
        
        UILabel * show = [[UILabel alloc] init];
        show.frame = CGRectMake(0, CGRectGetMaxY(showlabel.frame) + 20 * px, topView.width, topView.height);
        show.text = @"您确定终止该计划吗，终止后将无法恢复";
        show.textColor = [UIColor blackColor];
        show.font = [UIFont systemFontOfSize:WyzFont];
        show.textAlignment = NSTextAlignmentCenter;
        [topView addSubview:show];
        
        
        UIButton * mainbutton = [[UIButton alloc] init];
        mainbutton.frame = CGRectMake(0, showView.height - 150 * px, showView.width / 2, 150 * px);
        [mainbutton.layer setBorderColor:[UIColor grayColor].CGColor];
        [mainbutton.layer setBorderWidth:0.5];
        [mainbutton.layer setMasksToBounds:YES];
        [mainbutton setTitle:@"取消" forState:UIControlStateNormal];
        [mainbutton setTitleColor:wycMain forState:UIControlStateNormal];
        [mainbutton addTarget:self action:@selector(mainbuttonClick) forControlEvents:UIControlEventTouchUpInside];
        mainbutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [showView addSubview:mainbutton];
        
        
        
        UIButton * returebutton = [[UIButton alloc] init];
        returebutton.frame = CGRectMake(CGRectGetMaxX(mainbutton.frame), mainbutton.y, showView.width / 2, 150 * px);
        [returebutton.layer setBorderColor:[UIColor grayColor].CGColor];
        [returebutton.layer setBorderWidth:0.5];
        [returebutton.layer setMasksToBounds:YES];
        [returebutton setTitle:@"确定" forState:UIControlStateNormal];
        [returebutton setTitleColor:wycMain forState:UIControlStateNormal];
        [returebutton addTarget:self action:@selector(returebuttonClick) forControlEvents:UIControlEventTouchUpInside];
        returebutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [showView addSubview:returebutton];
    }
    
    if (lpGR.state == UIGestureRecognizerStateEnded)//手势结束
        
    {
        
        
        
    }
    
}
- (void)mainbuttonClick{
    self.showView.hidden = YES;
    self.ejectView.hidden = YES;
}
- (void)returebuttonClick{
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"token"] = [WYCAccountTool unarchivetoken];
    params[@"planId"] = planid;
    NSLog(@"终止计划传值%@",params);

    [HttpTool POST:consumeplanstopPlan params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSLog(@"终止计划传值%@",responseObject);
        NSString  *result = [responseObject objectForKey:@"result"];
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            
            [self mainbuttonClick];
            
            [self showToast:result];
            [self.tableView reloadData];

        }else{
            
            [self showToast:msgStr];
            
        }

        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {

        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
        
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    tableView.tableFooterView = [[UIView alloc] init];
    WYCPlandetaiLCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[WYCPlandetaiLCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell" HeightOfRow:50];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    WYCPlanModel * model = self.dataArray[indexPath.section];
    WYCPlanDetailModel  * PlanDetailModel = model.dataArray[indexPath.row];
    
    
    
    cell.timeLabel.text = PlanDetailModel.executeTime;
    if ([PlanDetailModel.type isEqualToString:@"1"]) {
//        double a = [PlanDetailModel.amount doubleValue] + [PlanDetailModel.poundage doubleValue];
                double a = [PlanDetailModel.amount doubleValue] ;

        cell.repaymentLbel.text = [NSString stringWithFormat:@"智能消费+%.2f",a];
        cell.repaymentLbel.textColor = [UIColor redColor];
        
        
    }else{
        
        double a = [PlanDetailModel.amount doubleValue] ;
        
        cell.repaymentLbel.text = [NSString stringWithFormat:@"智能还款-%.2f",a];
        cell.repaymentLbel.textColor = wycMain;
    }
    if ([PlanDetailModel.state isEqualToString:@"1"]) {
        cell.moneyLabel.text = [NSString stringWithFormat:@"未执行"];
        cell.moneyLabel.textColor = [UIColor blackColor];
        cell.moneyLabel.textAlignment = NSTextAlignmentRight;
    }else if ([PlanDetailModel.state isEqualToString:@"2"]){
        cell.moneyLabel.text = [NSString stringWithFormat:@"执行中"];
        cell.moneyLabel.textColor = [UIColor orangeColor];
        cell.moneyLabel.textAlignment = NSTextAlignmentRight;
        
    }else if ([PlanDetailModel.state isEqualToString:@"3"]){
        
        cell.moneyLabel.text = [NSString stringWithFormat:@"已执行"];
        cell.moneyLabel.textColor = [UIColor redColor];
        cell.moneyLabel.textAlignment = NSTextAlignmentRight;
    }else if ([PlanDetailModel.state isEqualToString:@"4"]){
        cell.moneyLabel.text = [NSString stringWithFormat:@"执行失败"];
        cell.moneyLabel.textColor = [UIColor blackColor];
        cell.moneyLabel.textAlignment = NSTextAlignmentRight;
    }else if ([PlanDetailModel.state isEqualToString:@"5"]){
        cell.moneyLabel.text = [NSString stringWithFormat:@"已终止"];
        cell.moneyLabel.textColor = [UIColor redColor];
        cell.moneyLabel.textAlignment = NSTextAlignmentRight;
    }else{
        cell.moneyLabel.text = [NSString stringWithFormat:@"执行失败终止"];
        cell.moneyLabel.textColor = [UIColor blackColor];
        cell.moneyLabel.textAlignment = NSTextAlignmentRight;
        
    }
    
    return cell;
}
@end

